Hi all,
I need to update the Status column in my Orders table to 'Completed' for all orders that
OrderDate are older than 30 days. Can someone provide the SQL query for this?
home / developersection / forums / writing a query to update records based on a condition in sql server
Hi all,
I need to update the Status column in my Orders table to 'Completed' for all orders that
OrderDate are older than 30 days. Can someone provide the SQL query for this?
Ravi Vishwakarma
16-Jul-2024To update the
Statuscolumn in yourOrderstable to 'Completed' for all orders where theOrderDateis older than 30 days, you can use theUPDATEstatement with aWHEREclause.Here is the query:
Explanation:
UPDATE Orders: Specifies the table to update.SET Status = 'Completed': Sets theStatuscolumn to 'Completed'.WHERE OrderDate < DATEADD(DAY, -30, GETDATE()): Filters the rows to update where theOrderDateis more than 30 days older than the current date.GETDATE(): Returns the current date and time.DATEADD(DAY, -30, GETDATE()): Subtracts 30 days from the current date, giving you the date 30 days ago.This query will update
Statusall orders placed more than 30 days ago to 'Completed'.Read more
How to Write a Query to Find the Second Highest Salary in SQL Server?
SQL Query to Pivot Data from Rows to Columns in SQL Server
Help with Writing a Query to Find Orphaned Records in SQL Server
How to Write a Query to Get Top N Records per Group in SQL Server?
SQL Query to Find Duplicate Records in a Table in SQL Server